<?php
/**
 * 家族管理
 */
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
class FamilyController extends AdminbaseController{
	protected $users_model,$role_model;
	
	function _initialize() {
		parent::_initialize();
	/*	$this->users_model = D("Common/Users");
		$this->role_model = D("Common/Role");*/
	}
	function index(){
	    $where='';
        if($_REQUEST['keyword']!=''){
            $where['name']	=array("like","%".$_REQUEST['keyword']."%");
        }
		$count=M('family')->count();
		$page = $this->page($count, 10);
		$users =M('family')
		->where($where)
		->order("uptime DESC")
		->limit($page->firstRow . ',' . $page->listRows)
		->select();
		foreach ($users as &$user){
		     $nums= M('family_member')->where('family_id='.$user['id'])->count();
		     if($nums) {
                 $user['nums']=$nums;
            }else{
                 $user['nums']=0;
             }
        }
		$this->assign("page", $page->show('Admin'));
		$this->assign("users",$users);
		$this->display();
	}
	
	
	function add(){

		$this->display();
	}
	
	function add_post(){
		if(IS_POST) {
                $data=M('family')->create();
                $map['name']=$data['name'];
                $is_family=M('family')->where($map)->find();
                if($is_family){
                    $this->error("家族名称重复！");
                }
                $map_m['user_login']=$data['zuzhang'];
                $is_member=M('users')->where($map_m)->find();
                if(!$is_member){
                    $this->error("没有找到族长账号！");
                }
                $data['uptime'] =time();
                $result = M('family')->add($data);
                if ($result !== false) {
                    $role_user_model = M("family");
                    $this->success("添加成功！", U("Family/index"));
                } else {
                    $this->error("添加失败！");
                }

        }

	}
	
	
	function edit(){
		$id= intval(I("get.id"));
		$users=M('family')->where("id=".$id)->find();
		$this->assign("userinfo",$users);
		$this->display();
	}
	
	function edit_post(){
		if (IS_POST) {
		    $id=$_POST['id'];
            $data=M('family')->create();
            $map['name']=$data['name'];
           /* $is_family=M('family')->where($map)->find();
            if($is_family){
                $this->error("家族名称重复！");
            }*/
            $map_m['user_login']=$data['zuzhang'];
            $is_member=M('users')->where($map_m)->find();
            if(!$is_member){
                $this->error("没有找到族长账号！");
            }
            $data['uptime'] =time();
            $result = M('family')->where('id='.$id)->save($data);
            if ($result !== false) {
                $this->success("编辑成功！", U("Family/index"));
            } else {
                $this->error("编辑失败！");
            }
			
		}
	}
	
	/**
	 *  删除
	 */
	function delete(){
		$id = intval(I("get.id"));
		if($id==1){
            $this->error("不能删除系统家族！");
        }
        M('family')->where(array("id"=>$id))->delete();
        $list=M('family_member')->where(array("family_id"=>$id))->select();
        foreach ($list as &$li){
            $map['family_id']=0;
            $map['family_name']='';
            M('users')->where('id='.$li['uid'])->save($map);
        }
        M('family_member')->where(array("family_id"=>$id))->delete();
        $this->success("删除成功！");
	}

    /**
     * 未参加家族主播
     */
	function memberinfo(){
        $id = intval(I("get.id"));
        $where='';
        if($_REQUEST['keyword']!=''){
            $where['user_login']	=array("like","%".$_REQUEST['keyword']."%");
        }
        if($_REQUEST['id']!=''){
            $id = $_REQUEST['id'];
        }
        $where['user_type']=2;
		$list=M('users')->where($where)->order('id desc')->select();
       // echo M('users')->getLastSql();  
        $family_name=M('family')->where('id='.$id)->getField('name');

		$this->assign('lists',$list);
        $this->assign('fam_id',$id);
        $this->assign('fam_name',$family_name);
		$this->display();
	}

    /**
     * 添加成员
     */
	function addmember(){
        $uid = intval(I("get.id"));
        $fam_id = intval(I("get.fam_id"));
        $fam_name=M('family')->where('id='.$fam_id)->getField('name');
        if(!$fam_name){
            $this->error("未找到家族！");
        }
        $map['uid']=$uid;
        $fam_mem=M('family_member')->where($map)->find();

        if(!$fam_mem){
            $data['uid'] = $uid;
            $data['family_id'] = $fam_id;
            $data['uptime'] = time();
            $res = M('family_member')->add($data);

        }else {
            $data['uid'] = $uid;
            $data['family_id'] = $fam_id;
            $data['uptime'] = time();
            $res = M('family_member')->where($map)->save($data);
        }


        $map['family_id']=$fam_id;
        $map['family_name']=$fam_name;
        //$map['bonus_time']=time();
        $r=M('users')->where('id='.$uid)->save($map);
        if($res){
            $this->success("添加成功！", U("Family/index"));
        }
    }

    /**
     * 查看成员
     */
    function  selmember(){
        $id = intval(I("get.id"));
        $list = M('family_member')->where('family_id='.$id)->select();
        foreach ($list as &$li){
            $li['user_login']=M('users')->where('id='.$li['uid'])->getField('user_login');
        }
        $family_name=M('family')->where('id='.$id)->getField('name');
        $this->assign('fam_name',$family_name);
        $this->assign('lists',$list);
        $this->assign('fam_id',$id);
        $this->display();
    }

    /**
     * 删除家族成员
     */
   function delmember(){
       $fam_id = intval(I("get.fam_id"));
       $uid = intval(I("get.id"));
       $res = M('family_member')->where('uid='.$uid)->delete();
       //echo M('family_member')->getLastSql();
       $map['family_id']=0;
       $map['family_name']='';
       $map['bonus_time']=time();
       $r=M('users')->where('id='.$uid)->save($map);
       //echo M('users')->getLastSql();
       if($res){
           $this->success("删除成功！", U("Family/index"));
       }
   }
	

	
}